package cn.edu.ctbu.kcsj.webapi;

import cn.edu.ctbu.kcsj.domain.Teacher;
import cn.edu.ctbu.kcsj.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/login")
public class LoginTeacherRestController {
    @Autowired
    TeacherService teacherService;

    @GetMapping("/teacher")
    public int user(HttpServletRequest httprequest, Integer tno, String password) {
        //判断用户名和密码是否存在
        List<Teacher> teachers = teacherService.findByTnoAndTpassword(tno, password);
        if (teachers.size() > 0) {
            //如果存在，写入session
            HttpSession session = httprequest.getSession();
            session.setAttribute("userid", teachers.get(0).getTno());
            session.setAttribute("user", teachers.get(0));
            return 1;//成功
        } else {
            return -1;//错误
        }

    }
}
